/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-08-19
 * Time: 21:38
 */
public class MySingleList implements IList{
    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;




    @Override
    public void reverse() {
        if(head == null){
            return;
        }

        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
    }

    @Override
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        head = node1;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public int middlePoint() {
        if(head == null){
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next !=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow.val;
    }

    @Override
    public int returns(int k) {
        int count = 0;
        ListNode fast = head;
        ListNode slow = head;
        while(count != k - 1){
            fast = fast.next;
            if(fast == null){
                return -1;
            }
            count++;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    @Override
    public boolean chkPalindrome() {
        if(head == null) return true;

        ListNode fast = head;
        ListNode slow = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        //2.翻转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }

        //3.判断回文
        while(head != slow){
            if(head.val != slow.val){
                return false;
            }
            //偶数情况
            if(head.next == slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pl = headA;
        ListNode ps = headB;
        int lenA = 0;
        int lenB = 0;
        //1.求链表的长度
        while(headA != null){
            headA = headA.next;
            lenA++;
        }
        while(headB != null){
            headB = headB.next;
            lenB++;
        }
        //2.求链表之间的长度差
        int len = lenA - lenB;
        if(len < 0){
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //3.长链表走差值
        while(len != 0){
            pl = pl.next;
            len--;
        }
        //4.判断链表值的交点
        while(pl.val != ps.val){
            pl = pl.next;
            ps = ps.next;
        }
        return ps;
    }
}
